package com.situ.day12;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

import org.junit.Test;

public class IODemo {

	public static void main(String[] args) {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("IO.txt");
			// 读取单个字符，如果已经到达流的末尾，则返回-1
			int ch1 = fileReader.read();
			System.out.println(ch1); // 97
			System.out.println((char) ch1);
			int ch2 = fileReader.read();
			System.out.println((char) ch2);
			int ch3 = fileReader.read();
			System.out.println((char) ch3);
			int ch4 = fileReader.read(); // -1
			System.out.println(ch4);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}

	@Test
	public void test1() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("IO.txt");
			// 将字符数读入数组buffer
			// 没有到底流的末尾，返回的是读取的字符个数
			// 如果已到达流的末尾，返回-1
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println("length = " + length);
				System.out.println(Arrays.toString(buffer));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) { // 防止空指针异常
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}

	@Test
	public void test2() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("IO.txt");
			// 复制IO.txt文件,复制的文件名为IO_Copy.txt
			fileWriter = new FileWriter("IO_Copy.txt");
			char[] buffer = new char[10];
			int length = 0;
			while ((length = fileReader.read(buffer)) != -1) {
				System.out.println("length = " + length);
				System.out.println(Arrays.toString(buffer));
				// 字符数组为buffer,从下标为0的地方开始写，要写入的字符数为length
				fileWriter.write(buffer, 0, length);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileWriter.close(); // 一般先后打开的文件先关闭
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Test
	public void test3() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader("IO.txt");
			char[] buffer = new char[10];
			// 初始假定为-1
			int ch = -1;
			// 用while循环去读，直到返回的值是-1，退出循环
			while ((ch = fileReader.read(buffer)) != -1) {
				System.out.println(Arrays.toString(buffer));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}

	@Test
	public void test4() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("02-02.jpg");
			fileOutputStream = new FileOutputStream("02-02_copy.jpg");
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length = fileInputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
